<template>
  <div class="products-page">
    <header class="header">
      <div class="container">
        <div class="logo">
          <h1>拼多多</h1>
        </div>
        <nav class="nav">
          <router-link to="/">首页</router-link>
          <router-link to="/products">商品</router-link>
          <router-link to="/cart">购物车</router-link>
          <template v-if="userStore.isLoggedIn">
            <el-dropdown @command="handleCommand" trigger="click">
              <span class="user-info">
                <el-icon><User /></el-icon>
                {{ userStore.user.username }}
                <el-icon class="el-icon--right"><arrow-down /></el-icon>
              </span>
              <template #dropdown>
                <el-dropdown-menu>
                  <el-dropdown-item command="admin" v-if="userStore.user.username === 'admin'">管理后台</el-dropdown-item>
                  <el-dropdown-item command="logout" :divided="userStore.user.username === 'admin'">退出登录</el-dropdown-item>
                </el-dropdown-menu>
              </template>
            </el-dropdown>
          </template>
          <template v-else>
            <router-link to="/login" class="login-link">登录/注册</router-link>
          </template>
        </nav>
      </div>
    </header>

    <div class="container">
      <div class="search-section">
        <h2 class="page-title">所有商品</h2>
        <div class="search-box">
          <el-input 
            v-model="searchKeyword" 
            placeholder="搜索商品..." 
            clearable
            @input="handleSearch"
          >
            <template #prefix>
              <el-icon><Search /></el-icon>
            </template>
          </el-input>
        </div>
      </div>
      <div class="product-list">
        <div 
          v-for="product in filteredProducts" 
          :key="product.id"
          class="product-item"
          @click="goToProduct(product.id)"
        >
          <div class="product-image">
            <img :src="product.image || '/placeholder.png'" :alt="product.name">
          </div>
          <div class="product-info">
            <h3>{{ product.name }}</h3>
            <p class="price">¥{{ product.price }}</p>
            <p class="stock">库存: {{ product.stock }}</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { Search, User, ArrowDown } from '@element-plus/icons-vue'
import { useUserStore } from '../stores'
import request from '../utils/request'

const router = useRouter()
const userStore = useUserStore()
const products = ref([])
const searchKeyword = ref('')

const handleCommand = (command) => {
  if (command === 'admin') {
    router.push('/admin/products')
  } else if (command === 'logout') {
    userStore.logout()
    ElMessage.success('已退出登录')
    router.push('/')
  }
}

const filteredProducts = computed(() => {
  if (!searchKeyword.value) {
    return products.value
  }
  const keyword = searchKeyword.value.toLowerCase()
  return products.value.filter(product => 
    product.name.toLowerCase().includes(keyword) ||
    (product.description && product.description.toLowerCase().includes(keyword))
  )
})

const handleSearch = () => {
  // 搜索逻辑已通过 computed 实现
}

const goToProduct = (id) => {
  router.push(`/products/${id}`)
}

onMounted(async () => {
  userStore.initUser()
  try {
    const res = await request.get('/products')
    products.value = res.data || []
  } catch (error) {
    console.error('加载商品失败:', error)
  }
})
</script>

<style scoped>
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}

.header {
  background: #fff;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  padding: 15px 0;
  margin-bottom: 40px;
}

.header .container {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.logo h1 {
  color: #E02E24;
  font-size: 24px;
}

.nav {
  display: flex;
  gap: 30px;
}

.nav a {
  text-decoration: none;
  color: #333;
  font-size: 16px;
}

.nav a.router-link-active {
  color: #E02E24;
}

.search-section {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 40px;
  gap: 30px;
}

.page-title {
  font-size: 32px;
  margin: 0;
  white-space: nowrap;
}

.search-box {
  flex: 1;
  max-width: 400px;
}

.product-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 30px;
}

.product-item {
  background: white;
  border-radius: 8px;
  overflow: hidden;
  cursor: pointer;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  transition: transform 0.3s;
}

.product-item:hover {
  transform: translateY(-5px);
}

.product-image {
  width: 100%;
  height: 200px;
  background: #f5f5f5;
  overflow: hidden;
}

.product-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.product-info {
  padding: 20px;
}

.product-info h3 {
  font-size: 16px;
  margin-bottom: 10px;
}

.price {
  color: #E02E24;
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 5px;
}

.stock {
  color: #666;
  font-size: 14px;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 5px;
  color: #E02E24;
  font-weight: 500;
  cursor: pointer;
  padding: 8px 15px;
  border-radius: 4px;
  transition: background-color 0.3s;
}

.user-info:hover {
  background-color: rgba(255, 103, 0, 0.1);
}

.login-link {
  color: #E02E24 !important;
  font-weight: 500;
}
</style>

